//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SwaptionVolCube2.h"
using namespace Cephei::QL::Termstructures::Volatility::Swaption;
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Indexes/SwapIndex.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/Swaption/SwaptionVolatilityCube.h>
using namespace Cephei::QL::Termstructures::Volatility;
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::CSwaptionVolCube2 (boost::shared_ptr<QuantLib::SwaptionVolCube2>& childNative, Object^ owner) : CSwaptionVolatilityCube(CSwaptionVolCube2::typeid)
{
#ifdef HANDLE
	_phSwaptionVolCube2 = NULL;
#endif
	_ppSwaptionVolCube2 = &childNative;
    _ppSwaptionVolatilityCube = new boost::shared_ptr<QuantLib::SwaptionVolatilityCube> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityCube> (*_ppSwaptionVolCube2));
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::CSwaptionVolCube2 (QuantLib::SwaptionVolCube2& childNative, Object^ owner) : CSwaptionVolatilityCube(CSwaptionVolCube2::typeid)
{
#ifdef HANDLE
	_phSwaptionVolCube2 = NULL;
#endif
	_ppSwaptionVolCube2 = new boost::shared_ptr<QuantLib::SwaptionVolCube2> (&childNative);
    _ppSwaptionVolatilityCube = new boost::shared_ptr<QuantLib::SwaptionVolatilityCube> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityCube> (*_ppSwaptionVolCube2));
    _SwaptionVolCube2Owner = owner;
    _SwaptionVolatilityCubeOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::CSwaptionVolCube2 (CSwaptionVolCube2^ copy) : CSwaptionVolatilityCube(CSwaptionVolCube2::typeid)
{
#ifdef HANDLE
	_phSwaptionVolCube2 = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwaptionVolCube2 = new boost::shared_ptr<QuantLib::SwaptionVolCube2> (copy->GetShared());
        _ppSwaptionVolatilityCube = new boost::shared_ptr<QuantLib::SwaptionVolatilityCube> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityCube> (*_ppSwaptionVolCube2));
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::CSwaptionVolCube2 (PLATFORM::Type^ t) : CSwaptionVolatilityCube(CSwaptionVolCube2::typeid)
{
#ifdef HANDLE
	_phSwaptionVolCube2 = NULL;
#endif
	if (!t->IsSubclassOf(CSwaptionVolCube2::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::CSwaptionVolCube2 (QuantLib::Handle<QuantLib::SwaptionVolCube2>& childNative, Object^ owner)  : CSwaptionVolatilityCube(CSwaptionVolCube2::typeid)
{
	_phSwaptionVolCube2 = &childNative;
	_ppSwaptionVolCube2 = &static_cast<boost::shared_ptr<QuantLib::SwaptionVolCube2>>(childNative.currentLink());
    _ppSwaptionVolatilityCube = new boost::shared_ptr<QuantLib::SwaptionVolatilityCube> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityCube> (*_ppSwaptionVolCube2));
    _SwaptionVolCube2Owner = owner;
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::CSwaptionVolCube2 (QuantLib::Handle<QuantLib::SwaptionVolCube2> childNative)  : CSwaptionVolatilityCube(CSwaptionVolCube2::typeid)
{
	_phSwaptionVolCube2 = &childNative;
	_ppSwaptionVolCube2 = &static_cast<boost::shared_ptr<QuantLib::SwaptionVolCube2>>(childNative.currentLink());
    _ppSwaptionVolatilityCube = new boost::shared_ptr<QuantLib::SwaptionVolatilityCube> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityCube> (*_ppSwaptionVolCube2));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::CSwaptionVolCube2 (QuantLib::SwaptionVolCube2 childNative)  : CSwaptionVolatilityCube(CSwaptionVolCube2::typeid)
{
#ifdef HANDLE
	_phSwaptionVolCube2 = NULL;
#endif
	_ppSwaptionVolCube2 = new boost::shared_ptr<QuantLib::SwaptionVolCube2> (new QuantLib::SwaptionVolCube2 (childNative));
    _ppSwaptionVolatilityCube = new boost::shared_ptr<QuantLib::SwaptionVolatilityCube> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityCube> (*_ppSwaptionVolCube2));
}
#endif

Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::~CSwaptionVolCube2 ()
{
    if (_ppSwaptionVolCube2 != NULL)
    {
	    delete _ppSwaptionVolCube2;
        _ppSwaptionVolCube2 = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::!CSwaptionVolCube2 ()
{
    if (_ppSwaptionVolCube2 != NULL)
    {
	    delete _ppSwaptionVolCube2;
    }
}
QuantLib::SwaptionVolCube2& Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::GetReference ()
{
    if (_ppSwaptionVolCube2 == NULL) throw REFNEW NativeNullException ();
	return **_ppSwaptionVolCube2;
}
boost::shared_ptr<QuantLib::SwaptionVolCube2>& Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::GetShared ()
{
    if (_ppSwaptionVolCube2 == NULL) throw REFNEW NativeNullException ();
	return *_ppSwaptionVolCube2;
}
QuantLib::SwaptionVolCube2* Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::GetPointer ()
{
    if (_ppSwaptionVolCube2 == NULL) throw REFNEW NativeNullException ();
	return &**_ppSwaptionVolCube2;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SwaptionVolCube2>& Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::GetHandle ()
{
	if (_phSwaptionVolCube2 == NULL)
	{
		_phSwaptionVolCube2 = new Handle<QuantLib::SwaptionVolCube2> (*_ppSwaptionVolCube2);
	}
	return *_phSwaptionVolCube2;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::HasNative () 
{
	return (_ppSwaptionVolCube2 != NULL);
}

Cephei::QL::Termstructures::Volatility::Swaption::ISwaptionVolCube2^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::PerformCalculations::get ()
{
    try
    {
    	(*_ppSwaptionVolCube2)->performCalculations ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::ISmileSection^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::SmileSectionImpl (Double optionTime, Double swapLength)
{
    try
    {
        QuantLib::Time _optionTime = (QuantLib::Time)ValueHelper::Convert (optionTime); //a
        QuantLib::Time _swapLength = (QuantLib::Time)ValueHelper::Convert (swapLength); //a
    	boost::shared_ptr<QuantLib::SmileSection> _rv = (boost::shared_ptr<QuantLib::SmileSection>)(*_ppSwaptionVolCube2)->smileSectionImpl ( _optionTime,  _swapLength );   
        Cephei::QL::Termstructures::Volatility::CSmileSection^ _nrv = REFNEW Cephei::QL::Termstructures::Volatility::CSmileSection (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::ISmileSection^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::SmileSectionImpl (DateTime optionDate, Cephei::QL::Times::IPeriod^ swapTenor)
{
    CPeriod^ _CswapTenor;
    try
    {
        QuantLib::Date _optionDate = (QuantLib::Date)ValueHelper::Convert (optionDate); //a
        _CswapTenor = safe_cast<CPeriod^> (swapTenor);
        _CswapTenor->Lock();
        QuantLib::Period& _swapTenor = static_cast<QuantLib::Period&> (_CswapTenor->GetReference ()); 
    	boost::shared_ptr<QuantLib::SmileSection> _rv = (boost::shared_ptr<QuantLib::SmileSection>)(*_ppSwaptionVolCube2)->smileSectionImpl ( _optionDate,  _swapTenor );   
        Cephei::QL::Termstructures::Volatility::CSmileSection^ _nrv = REFNEW Cephei::QL::Termstructures::Volatility::CSmileSection (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CswapTenor != nullptr) _CswapTenor->Unlock();
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolCube2::VolSpreads (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppSwaptionVolCube2)->volSpreads ( _i );   
        Cephei::QL::Math::CMatrix^ _nrv = REFNEW Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

